const state = {
    cartList: uni.getStorageSync('cartList') || []
}
const mutations = {
    addCart(state, goods) {
        const index = state.cartList.findIndex(item => item.goods_id === goods.goods_id)
        if (index === -1) {
            state.cartList.push({
                ...goods,
                nums: 1,
                isSelected: false
            })
        } else {
            state.cartList[index].nums++
        }
        uni.setStorageSync('cartList', state.cartList)
    },
    // 减少state中商品数量
    reduceNum(state, index) {
        state.cartList[index].nums--
        if (state.cartList[index].nums <= 0) {
            state.cartList.splice(index, 1)
        }
        uni.setStorageSync('cartList', state.cartList)
    },
    addNum(state, index) {
        state.cartList[index].nums++
        uni.setStorageSync('cartList', state.cartList)
    },
    isSelected(state, index) {
        state.cartList[index].isSelected = !state.cartList[index].isSelected
        uni.setStorageSync('cartList', state.cartList)
    },
    isAllSelected(state,allSelected) {
        state.cartList.map(item => item.isSelected = allSelected)
        uni.setStorageSync('cartList', state.cartList)
    }
}
const actions = {}

export default {
    namespaced: true,
    state,
    mutations,
    actions,
}